package code.lucamarrocco.rest;

import java.util.LinkedList;
import java.util.List;

public class Methods {

	public static final List<String> methodsOf(RestfulProtocol protocol) {
		List<String> strings = new LinkedList<String>();

		for (RestfulMethod method : protocol.getMethods()) {
			strings.add(method.toString());
		}

		return strings;
	}

	public static final String methodOf(String queryString) {
		if (queryString == null) return "";
		if (queryString.matches("^[^=&]*$")) return "!" + queryString;
		if (queryString.matches("^[^=&]*&*$")) return "!" + queryString.replaceAll("&", "");
		if (queryString.matches("^&*[^=&]*&*$")) return "!" + queryString.replaceAll("&", "");
		return "";
	}

	public static final List<String> resourcesOf(RestfulProtocol protocol) {
		List<String> strings = new LinkedList<String>();

		for (RestfulResource resource : protocol.getResources()) {
			strings.add(resource.getName());
		}

		return strings;
	}
}
